/*
 * JDialogImage.java
 *
 * Created on 12-may-2012, 10:54:27
 */
package com.ufps.cuatro.vistas.representacion;
 
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.tree.DefaultMutableTreeNode;

import org.jdesktop.application.Action;

import com.ufps.cuatro.CuatroApp;
import com.ufps.cuatro.controlador.render.UtilRender;
import com.ufps.cuatro.modelo.biblioteca.StaticListModelObject;
import com.ufps.cuatro.modelo.representacion.UtilRepresentacion;
import com.ufps.cuatro.modelo.representacion.componente.ComponenteGenerado;
import com.ufps.cuatro.util.Bandera;
import com.ufps.cuatro.util.InformacionNodo;
import com.ufps.cuatro.util.MessageWindow;
import com.ufps.cuatro.util.Utileria;
import com.ufps.cuatro.util.generadores.GeneradorArbol;
import com.ufps.cuatro.vistas.layout.JPanelExplorerTreeView;

/**
 *
 * @author Catc
 */
public class JDialogSelectModelStaticObject extends javax.swing.JDialog implements MessageWindow{
    private static final long serialVersionUID = 4620237990680611593L;    
    
    private Object objParent;            
    private StaticListModelObject objStaticListModelObject;
    private ComponenteGenerado objComponenteGenerado;
    private Object objSeleccionado;
    private Object objModeloSeleccionado;

    /** Creates new form JDialogImage */
    public JDialogSelectModelStaticObject(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();        
    }   
    
    public JDialogSelectModelStaticObject(Object objParent, ComponenteGenerado objComponenteGenerado) {
        super(CuatroApp.getApplication().getMainFrame(), true);
        initComponents();
        this.objParent = objParent;        
        this.objComponenteGenerado = objComponenteGenerado;        
        UtilRender.helperIconLabelComponente(jLabelComponente, objComponenteGenerado);
        crearEstructuraModelsStatics();
        if(objComponenteGenerado.getStaticListModelString() != null){
            objStaticListModelObject = objComponenteGenerado.getStaticListModelString();
            UtilRender.helperIconLabel(jLabelHelp, objStaticListModelObject);
        }
    }    
    
    private void crearEstructuraModelsStatics(){
        try{
            DefaultMutableTreeNode objRaiz =
            GeneradorArbol.crearEstructuraBibliotecaModelsStatics("Modelos Estaticos", UtilRepresentacion.objBibliotecaTrabajoLocal);            
            if(objRaiz == null){                    
                objRaiz = GeneradorArbol.crearNodo("Vista No Disponible.");
            }
            UtilRender.helperRenderJTree(jTreeModelsStatic, objRaiz, Utileria.ARBOL_ESTRUCTURA_MODEL_STATIC, true, 0);            
        } catch (java.lang.NullPointerException ex) {
            Logger.getLogger(JDialogSelectModelStaticObject.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jToolBar1 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jButton3 = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        jButton4 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTreeModelsStatic = new javax.swing.JTree();
        jLabelComponente = new javax.swing.JLabel();
        jLabelHelp = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Selección Modelos Estáticos");
        setResizable(false);

        jLabel2.setBackground(new java.awt.Color(255, 255, 255));
        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ufps/cuatro/resources/icono app x 32.png"))); // NOI18N
        jLabel2.setText("<html><p><b>Modelos Estáticos Disponibles:</b></p><p>Seleccione un Modelo para Asignarlo al Componente.</p>");
        jLabel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel2.setName("jLabel2"); // NOI18N
        jLabel2.setOpaque(true);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.setName("jToolBar1"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(JDialogSelectModelStaticObject.class, this);
        jButton1.setAction(actionMap.get("aceptar")); // NOI18N
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setName("jButton1"); // NOI18N
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton1);

        jSeparator1.setName("jSeparator1"); // NOI18N
        jToolBar1.add(jSeparator1);

        jButton3.setAction(actionMap.get("cancelar")); // NOI18N
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton3.setName("jButton3"); // NOI18N
        jButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton3);

        jSeparator3.setName("jSeparator3"); // NOI18N
        jToolBar1.add(jSeparator3);

        jButton4.setAction(actionMap.get("help")); // NOI18N
        jButton4.setFocusable(false);
        jButton4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton4.setName("jButton4"); // NOI18N
        jButton4.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton4);

        jLabel1.setText("Componente:");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel3.setText("Lista de Modelos Estáticos:");
        jLabel3.setName("jLabel3"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTreeModelsStatic.setName("jTreeModelsStatic"); // NOI18N
        jTreeModelsStatic.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                jTreeModelsStaticValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jTreeModelsStatic);

        jLabelComponente.setBackground(new java.awt.Color(255, 255, 255));
        jLabelComponente.setText("jLabel4");
        jLabelComponente.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabelComponente.setName("jLabelComponente"); // NOI18N
        jLabelComponente.setOpaque(true);

        jLabelHelp.setBackground(new java.awt.Color(255, 255, 225));
        jLabelHelp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ufps/cuatro/resources/crystal_clear_icons/png/16x16/apps/ktip.png"))); // NOI18N
        jLabelHelp.setText("<html></html>");
        jLabelHelp.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabelHelp.setName("jLabelHelp"); // NOI18N
        jLabelHelp.setOpaque(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabelComponente, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addContainerGap(187, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelHelp, javax.swing.GroupLayout.DEFAULT_SIZE, 306, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 306, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabelComponente))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelHelp, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTreeModelsStaticValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTreeModelsStaticValueChanged
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) (evt.getPath().getLastPathComponent());
        Object nodeInfo = node.getUserObject();
        InformacionNodo objNodoSeleccionado = (InformacionNodo) nodeInfo;
        try {            
            this.objSeleccionado = objNodoSeleccionado.getObjElemento();
            if (node.isRoot()) {
                //El nodo seleccionado es la raiz del arbol                
            } else if (node.isLeaf()) {
                //El nodo seleccionado es una hoja                
            } else {
                //El nodo seleccionado es una rama                
            }              
            if(objSeleccionado instanceof StaticListModelObject){
                objModeloSeleccionado = objSeleccionado;                
                UtilRender.helperIconLabel(jLabelHelp, objModeloSeleccionado);
            }
        } catch (java.lang.ClassCastException ex) {
            Logger.getLogger(JDialogSelectModelStaticObject.class.getName()).log(Level.SEVERE, null, ex);            
        }
    }//GEN-LAST:event_jTreeModelsStaticValueChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JDialogSelectModelStaticObject.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JDialogSelectModelStaticObject.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JDialogSelectModelStaticObject.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JDialogSelectModelStaticObject.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                JDialogSelectModelStaticObject dialog = new JDialogSelectModelStaticObject(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    @Action
    public void aceptar() {                
        if(objModeloSeleccionado == null){
            msgWarning("Debe Seleccionar un Modelo.");
            return;
        }        
        if(msgConfirm("¿Acepta el Modelo Seleccionado?")){
            objComponenteGenerado.setStaticListModelString((StaticListModelObject) objModeloSeleccionado);
            Bandera objBandera = new Bandera();
            objBandera.addObserver((Observer) objParent);
            objBandera.setEstado(Bandera.STATIC_LIST_MODEL_SELECTED);                
            objBandera.setFlag(true);
            objBandera.setObj(objModeloSeleccionado);
            objBandera.notificar();      
            dispose();
        }
    }

    @Action
    public void incluirImg() {        
    }

    @Action
    public void cancelar() {
        if(msgConfirm("¿Desea Cancelar el Proceso?")){
            Bandera objBandera = new Bandera();
            objBandera.addObserver((Observer) objParent);
            objBandera.setEstado(Bandera.PROCESO_CANCELADO);                
            objBandera.setFlag(false);
            objBandera.setObj(null);
            objBandera.notificar(); 
            dispose();
        }
    }

    @Action
    public void help() {
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabelComponente;
    private javax.swing.JLabel jLabelHelp;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTree jTreeModelsStatic;
    // End of variables declaration//GEN-END:variables

    public boolean msgConfirm(String strMsg) {
        if(objParent instanceof JPanelExplorerTreeView){
            JPanelExplorerTreeView objPanel = (JPanelExplorerTreeView) objParent;
            return UtilRender.msgConfirmar(strMsg, objPanel);            
        }
        else{
            return UtilRender.msgConfirmar(strMsg, this);            
        }        
    }

    public void msgInfo(String strMsg) {
        UtilRender.msgInformacion(strMsg, this);                    
    }

    public void msgWarning(String strMsg) {        
        UtilRender.msgAdvertencia(strMsg, this);                    
    }

    public void msgError(String strMsg) {        
        UtilRender.msgError(strMsg, this);                    
    }

    @Action
    public void incluir() {        
    }

    @Action
    public void excluir() {        
    }

    @Action
    public void limpiar() {        
    }

    @Action
    public void up() {        
    }

    @Action
    public void upAll() {        
    }

    @Action
    public void downAll() {        
    }

    @Action
    public void down() {        
    }
}
